package cn.edu.scau.cmi.linjinyong.mybatis.dao.impl;

import cn.edu.scau.cmi.linjinyong.mybatis.dao.StudentDao;
import cn.edu.scau.cmi.linjinyong.mybatis.domain.Student;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;

public class StudentDaoImpl implements StudentDao {
    private SqlSession sqlSession;
    @Override
    public List<Student> listStudents() {
        return sqlSession.selectList("cn.edu.scau.cmi.linjinyong.mybatis.dao.StudentDao.listStudents");
    }

    @Override
    public boolean deleteStudentById(long id) {
        return sqlSession.delete("cn.edu.scau.cmi.linjinyong.mybatis.dao.StudentDao.deleteStudentById", id)>0;
    }

    @Override
    public Student getStudentById(long id) {
        return sqlSession.selectOne("cn.edu.scau.cmi.linjinyong.mybatis.dao.StudentDao.getStudentById", id);
    }

    @Override
    public boolean addStudent(Student student) {
        return sqlSession.insert("cn.edu.scau.cmi.linjinyong.mybatis.dao.StudentDao.addStudent", student)>0;
    }

    @Override
    public boolean updateStudent(Student student) {
        return sqlSession.update("cn.edu.scau.cmi.linjinyong.mybatis.dao.StudentDao.updateStudent", student)>0;
    }

    @Override
    public boolean chooseInstructor(long sid, long tid) {
        HashMap<String, Long> data = new HashMap<>();
        data.put("sid", sid);
        data.put("tid", tid);
        return sqlSession.update("cn.edu.scau.cmi.linjinyong.mybatis.dao.StudentDao.chooseInstructor", data)>0;
    }

    @Override
    public boolean chooseStudent(long sid) {
        return sqlSession.update("cn.edu.scau.cmi.linjinyong.mybatis.dao.StudentDao.chooseStudent", sid)>0;
    }

    @Override
    public List<Student> listSelectableStudentsByTid(long tid) {
        return sqlSession.selectList("cn.edu.scau.cmi.linjinyong.mybatis.dao.StudentDao.listSelectableStudentsByTid", tid);
    }

    public SqlSession getSqlSession() {
        return sqlSession;
    }

    public void setSqlSession(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }
}
